﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Linq;
using WPFExcelReport.Helpers;
using System.Windows.Input;
using System.Windows.Controls;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// 专用于坯布检验
    /// 1.扫描布卷号，进入验布，读取生产单信息
    /// 2.输入长度，修改数据库记录
    /// 3.剪出新布卷
    /// 4.打印标签
    /// 5.疵点保存修正
    /// 6.事件记录
    /// 7.纬纱扣除
    /// 8.织轴扣除
    /// </summary>
    public partial class TSHWQI03
    {
       
        public TSHWQI03()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBBST();
            this.DataContext = MV;
            InitializeComponent(); 
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            base.Window_Loaded(sender, e);
            this.stopwatch.Start();
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            if (IsEdit)
            {
                SaveCommand(null, null);
                PM["QIMACHINE"] = MACHINEID;
                TMV.RVZEROWB(PM);
            }
            STKNR = "";
            TBMain.Clear();
            base.QueryCommand(sender, e);
        }
        public override void OnSTKNRChg(string value)
        {
            if (STKNR == "16301901")
                return;
            if (STKNR == "")
                return;
            Query();
            IsBusy = false;
            base.OnSTKNRChg(value);
        }


        //如果生产单对应的已经在系统内存在，则不在创建
        //否则创建DI单，表明品种总数量
        //创建PR单，表明织机上的卡
        public override void INTEXVALIDATE()
        {
            try
            {


                if (TBMain.Rows.Count > 0)
                    return;
                IsBusy = true;
                SResult = QEXTSOURCE(PM);
                if (SResult.Tables[0].Rows.Count == 0)
                    return;
                WFART = "";
                WAART = "";
                EXORD = "";
                //WBBVAR  WBBFARBE WBBBESCHICH WBBDESS WBBCOLORIT WABEZ
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    WFART = "F" + Helper.Left(row["WBBART"].ToString().PadRight(20), 20)
                        + Helper.Left(row["WBBVAR"].ToString().PadRight(2), 2)
                        + Helper.Left(row["WBBFARBE"].ToString().PadRight(10), 10)
                        + Helper.Left(row["WBBBESCHICH"].ToString().PadRight(10), 10)
                         + Helper.Left(row["WBBDESS"].ToString().PadRight(10), 10)
                        + Helper.Left(row["WBBCOLORIT"].ToString().PadRight(10), 10)
                           + "".PadRight(7)
                        + Helper.Left(Helper.ConvertGB2312To8859P1(row["WABEZ"].ToString().PadRight(20)), 20);
                    WAART = WFART.Substring(0, 23);
                    DES = Helper.Left(Helper.ConvertGB2312To8859P1(row["WABEZ"].ToString().PadRight(20)), 20);
                    PARTIE = Helper.Left(row["PARTIE"].ToString().PadRight(10), 10);
                    EXORD = Helper.Left(row["PRODORDER"].ToString().PadRight(11), 11)
                       + WFART
                        + Helper.Left(row["KDKUNDNA"].ToString().PadRight(8), 8)
                        + Helper.Left(row["LABELTYP"].ToString().PadRight(20), 20)
                        + Helper.Left(row["PARTIE"].ToString().PadRight(10), 10);
                    break;
                }
                base.INTEXVALIDATE();
                IsBusy = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        //通过外部数控查询，获取品种，销售单，生产单，织机机台等相关信息
        private DataSet QEXTSOURCE(Dictionary<string, object> pm)
        {
            try
            { 
                string sql = " AND MC.MachineName = " + LOOMID;
                //SResult = TMV.QWBEXRTJK(sql);
                SResult = TMV.QWBEXDEMO(sql);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);

            }
            return SResult;

        }  
    }
}
